home *** CD-ROM | disk | FTP | other *** search
- Path: news.delcoelect.com!usenet
- From: c23kab@kocrsv08.delcoelect.com (Kirk Bailey)
- Newsgroups: comp.lang.c
- Subject: Evaluation Requirement Question
- Date: 15 Mar 1996 20:41:22 GMT
- Organization: Delco Electronics Corp.
- Distribution: world
- Message-ID: <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
- NNTP-Posting-Host: kocrsw25.delcoelect.com
-
- In the following example program, notice that the
- test for "i" appears both before and after the call to "foo".
- Note that the printf will only be executed (in this example)
- due to the last "i != 0" check (i is initialized to 0, and
- foo will return 1). My question: Is the compiler _required_
- to evaluate "i" twice in all cases where it can not determine
- that the call to "foo" did not alter "i"? Are there cases
- where "i" doesn't have to be evaluated twice? Consider some
- other possible programs:
-
- 1) foo did not _change_ i, or
- 2) foo did not _change_ i AND foo's definition occurred
- before its invocation (allowing the compiler to "know"
- this at the point of invocation), or
- 3) "i" was a local variable to main (explicitly init. to
- zero) - meaning that no function outside of main could
- alter it through the call to foo.
-
-
- #include <stdio.h>
-
- int i;
- int foo(void);
-
- int main(void)
- {
- if ( i != 0 || !foo() || i != 0 )
- printf( "got here!\n" );
- return 0;
- }
-
- int foo(void)
- {
- i++;
- return i;
- }
-
- --
- Kirk Bailey (317) 451-0807
- Software Engineer, Simulation Tools
- Delco Electronics Corporation
-